﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// SearchBox.xaml 的交互逻辑
    /// </summary>
    public partial class SearchBox : UserControl
    {
        #region Accessor
        public static readonly DependencyProperty IsSearchDirectlyProperty =
            DependencyProperty.Register("IsSearchDirectly", typeof(bool), typeof(SearchBox), new PropertyMetadata(true));

        [Description("获取或设置数是否启用立即查询")]
        [Category("其他")]
        public bool IsSearchDirectly
        {
            get { return Convert.ToBoolean(this.GetValue(IsSearchDirectlyProperty)); }
            set { this.SetValue(IsSearchDirectlyProperty, value); }
        }

        /// <summary>
        /// 关键字改变事件
        /// </summary>
        public event KeyEventHandler KeywordsChanging = null;

        /// <summary>
        /// 关键字重置事件
        /// </summary>
        public event EventHandler KeywordsReset = null;
        #endregion

        #region Public functions
        /// <summary>
        /// 获取关键字
        /// </summary>
        public string Keywords
        {
            get { return txbKeyword.Text; }
        }
        #endregion

        #region Private functions
        private void txbKeyword_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (IsSearchDirectly ||
                (!IsSearchDirectly && e.Key == Key.Enter))
            {
                if (string.IsNullOrEmpty(txbKeyword.Text))
                {
                    OnKeywordsReset(new EventArgs());
                }
                else
                {
                    OnKeywordsChanging(e);
                }
            }
        }

        /// <summary>
        /// 触发关键字改变事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnKeywordsChanging(KeyEventArgs e)
        {
            if (KeywordsChanging != null)
            {
                KeywordsChanging(this, e);
            }
        }

        /// <summary>
        /// 触发关键字重置事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnKeywordsReset(EventArgs e)
        {
            if (KeywordsReset != null)
            {
                KeywordsReset(this, e);
            }
        }
        #endregion

        #region Structure
        /// <summary>
        /// 创建搜索框控件
        /// </summary>
        public SearchBox()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        #endregion
    }
}
